package com.example.config;

import com.example.interceptor.VisitInterceptor;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Web配置类，用于配置项目中通用的Bean
 *
 * @auth shanyue
 * @date 2025年06月19日15:20
 */
@Configuration
public class WebConfiguration implements WebMvcConfigurer {

    @Resource
    private VisitInterceptor visitInterceptor;

    /**
     * 注册BCryptPasswordEncoder Bean到容器中
     * SpringSecurity登录时会自动使用该加密器进行密码匹配
     *
     * @return 实例
     */
    @Bean
    BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    /**
     * 添加自定义拦截器到SpringMvc拦截器中
     *
     * @param registry 拦截器注册器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(visitInterceptor)
                .addPathPatterns("/**") //拦截所有请求
                .excludePathPatterns("/api/auth/**", "/error"); //排除登录、错误路径
    }
}
